package com.hushui.mall.commodity.controller;

import java.util.Arrays;
import java.util.Map;

import com.hushui.mall.commodity.entity.SkuInfoEntity;
import com.hushui.mall.commodity.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hushui.mall.commodity.entity.SpuInfoEntity;
import com.hushui.mall.commodity.service.SpuInfoService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * spu信息
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:45
 */
@RestController
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 列表
     */
    @RequestMapping("commodity/spuinfo/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("commodity/spuinfo/info/{id}")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/spuinfo/save")
    public R save(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.save(spuInfo);

        return R.ok();
    }
    @GetMapping("commodity/spuinfo/info/{id}")
    public R getSpuInfoBySkuId(@PathVariable(value = "id") Long id){
        SkuInfoEntity byId = skuInfoService.getById(id);
        SpuInfoEntity byId1 = spuInfoService.getById(byId.getSpuId());
        return R.ok().put("spuInfo",byId1);
    }
    /**
     * 修改
     */
    @RequestMapping("commodity/spuinfo/update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }
    @RequestMapping("/product/spuinfo/{spuId}/up")
    public R spuUp(@PathVariable(value = "spuId")Long spuId){
//        SpuInfoEntity byId = spuInfoService.getById(spuId);
//        byId.setPublishStatus(1);
//        spuInfoService.updateById(byId);
        //上架是把需要检索的商品放到ES数据库中
        boolean up = spuInfoService.up(spuId);
        if(!up){
            return R.error(10005,"上架失败");
        }
        return R.ok();


    }
    @RequestMapping("/product/spuinfo/list")
    public R getSpu(@RequestParam Map<String, Object> params){

        if (params.get("brandId") ==null &&params.get("status") ==null && params.get("catelogId") ==null){
            Long brandId = new Long(0);
            Long catelogId = new Long(0);
            Integer status = new Integer(0);
            PageUtils page = spuInfoService.querySpuPage(params,brandId,status,catelogId);
            return R.ok().put("page", page);
        }else {
            Long brandId = (Long) params.get("brandId");
            Long catelogId = (Long) params.get("catelogId");
            Integer status = (Integer) params.get("status");
            PageUtils page = spuInfoService.querySpuPage(params,brandId,status,catelogId);
            return R.ok().put("page", page);
    }}
    /**
     * 删除
     */
    @RequestMapping("commodity/spuinfo/delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
